package com.pn.service.impl;

import com.pn.entity.ProductType;
import com.pn.entity.Result;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.pn.mapper.ProductTypeMapper;
import com.pn.service.ProductTypeService;

import java.util.ArrayList;
import java.util.List;

@CacheConfig(cacheNames = "com.pn.service.impl.ProductTypeServiceImpl")
@Service
public class ProductTypeServiceImpl implements ProductTypeService{

    @Resource
    private ProductTypeMapper productTypeMapper;

    @Cacheable(key = "'all:typeTree'")
    @Override
    public List<ProductType> findProductTypeTree() {
        List<ProductType> productTypes = productTypeMapper.selectAllProduct();
        List<ProductType> productTypeTree = productTypeToTree(productTypes, 0);
        return productTypeTree;
    }

    @Override
    public Result findProductTypeCode(String typeCode) {
        ProductType productType = productTypeMapper.selectTypeCode(typeCode);
        return Result.ok(productType == null);
    }

    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result addProductType(ProductType productType) {
        ProductType selectProductTypeName = productTypeMapper.selectProductTypeName(productType.getTypeName());
        if (selectProductTypeName == null) {
            int result = productTypeMapper.insertProductType(productType);
            if (result > 0) {
                return Result.ok("产品分类添加成功！");
            }
            return Result.err(Result.CODE_ERR_BUSINESS, "产品分类添加失败！");
        }else {
            return Result.err(Result.CODE_ERR_BUSINESS,"产品分类名称已经存在！");
        }
    }
    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result removeProductType(Integer typeId) {
           int result = productTypeMapper.deleteProductTypeByTid(typeId);
        if (result > 0){
            return Result.ok("产品类型删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"产品类型删除失败！");
    }

    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result modifyProductType(ProductType productType) {
        ProductType productTypeName = productTypeMapper.selectProductTypeName(productType.getTypeName());
        if (productTypeName != null && !productTypeName.getTypeId().equals(productType.getTypeId())){
            return Result.err(Result.CODE_ERR_BUSINESS,"产品类型更新失败！");
            }else {
            int result = productTypeMapper.updateProductType(productType);
            if (result > 0){
                return Result.ok("产品类型更新成功！");
        }
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"产品类型名称已存在！");
    }

    //将产品分类集合转转换成产品分类树
    private List<ProductType> productTypeToTree(List<ProductType> allProductType,Integer pid){
        List<ProductType> firstProductType = new ArrayList<>();
        for (ProductType productType:allProductType
        ) {
            if (productType.getParentId().equals(pid)){
                firstProductType.add(productType);
            }
        }
        for (ProductType productType:firstProductType
        ) {
            List<ProductType> secondProductType = productTypeToTree(allProductType, productType.getTypeId());
            productType.setChildProductCategory(secondProductType);
        }
        return firstProductType;
    }











}
